import myAxios from '../myAxios/myAxios.js';
import myAxios28 from '../myAxios/myAxios28.js';

let GetSuJiRequire = async function () {
	await UpdateMaterial();
	const testMatnr = "E0010108X";

	// 获取CGData列表
	const CGDataList = (await GetCGDData(companyList)).data.resultList;
	const CGDatacc = CGDataList.filter(item => item.MATNR === testMatnr);

	// 查询数据库
	const sqlTable = await myAxios.get("陈帅廷查询周计划_驱动器物料分类", '', 'Q902');

	// 获取塑机需求物料列表
	const matnrList = CGDataList.map(item => {
		// 有些物料号是纯数字,js认为是数字类型,会没有startsWith函数导致报错,所以用之前都转换成string
		item.MATNR = item.MATNR.toString();
		if ((item.MATNR && item.MATNR.startsWith("E00")) ||
			item.TXZ01.includes("电气") ||
			item.TXZ01.includes("配电箱") ||
			item.TXZ01.includes("驱动器") ||
			item.TXZ01.includes("卡") ||
			item.TXZ01.includes("电容") ||
			item.TXZ01.includes("电源组件") ||
			item.TXZ01.includes("电阻") ||
			item.TXZ01.includes("三相") ||
			item.TXZ01.includes("机壳") ||
			item.TXZ01.includes("垫圈") ||
			item.TXZ01.includes("J5") ||
			item.TXZ01.includes("操作器") ||
			item.TXZ01.includes("传感器") ||
			item.TXZ01.includes("接头（套）") ||
			item.TXZ01.includes("驱动板") ||
			item.TXZ01.includes("IGBT模块") ||
			item.TXZ01.includes("主板")) {
			return item.MATNR;
		} else {
			const rows = sqlTable.filter(row => row.NAME === item.MATNR);
			if (rows.length > 0) {
				const typeName = rows[0].PROPERTIES;
				return typeName === "驱动器" ? item.MATNR : "";
			}
			return "";
		}
	}).filter(str => str !== "");

	const stringc = matnrList.find(item => item === testMatnr);

	// 分组处理
	let BBList = CGDataList.filter(item => matnrList.includes(item.MATNR)).reduce((groups, item) => {
		const key = item.MATNR + "曐" + item.TXZ01;
		if (!groups[key]) {
			groups[key] = {
				MATNR: item.MATNR,
				MAKTX: item.TXZ01,
				REQUIRE_SUJI: 0
			};
		}
		groups[key].REQUIRE_SUJI += parseInt(item.DYQUAN || 0);;
		return groups;
	}, {});
	// console.log('object :>> ', BBList);
	BBList = Object.values(BBList);

	const c = BBList.filter(item => item.MATNR === testMatnr);

	// 处理动力单元
	let tempList = [];
	for (let i = 0; i < BBList.length; i++) {
		const citem = BBList[i];

		if (citem.MAKTX.includes("动力")) {
			const TYPE = await myAxios.getSap("SAP标准BOM(不展开)", `Q902🈹${citem.MATNR}`, "9911");
			const rows = TYPE.TableSet.ZBBOM || [];

			if (rows.length > 0) {
				for (let j = 0; j < rows.length; j++) {
					let qty;
					try {
						qty = citem.REQUIRE_SUJI * parseInt(rows[j].MENGE || 1);
					} catch {
						qty = citem.REQUIRE_SUJI * 1;
					}

					tempList.push({
						MATNR: rows[j].IDNRK || "",
						MAKTX: rows[j].MAKTX || "",
						REQUIRE_SUJI: qty
					});
				}
			}
		} else {
			tempList.push(citem);
		}
	}

	BBList = tempList;

	// 最终分组
	BBList = BBList.reduce((groups, item) => {
		if (!groups[item.MATNR]) {
			groups[item.MATNR] = {
				MATNR: item.MATNR,
				REQUIRE_SUJI: 0
			};
		}
		groups[item.MATNR].REQUIRE_SUJI += item.REQUIRE_SUJI;
		return groups;
	}, {});

	BBList = Object.values(BBList);

	const BBLi111 = BBList.filter(item => item.MATNR === testMatnr);

	// 转换为DataTable格式
	const table = [...BBList];
	const rowsSS = table.filter(row => row.MATNR === testMatnr);
	window.sujiTable = table;
	console.log('GetSuJiRequire :>> ', table);
	return table;
};

///更新物料
let UpdateMaterial = function () {
	return myAxios.post("陈帅廷更新MATTYPE", "", 'Q902');
};
let companyList = [
	{ COMPANY: "精工", LIFNR: "0010000556", SUUID: "81C340CE4A4A1FD095F8E5ACA98C02F8", },
	// { COMPANY: "安信", LIFNR: "0010000925", SUUID: "005056B2A1AC1EEB8586FD178B1BD83D", },
	// { COMPANY: "博舜", LIFNR: "0010001344", SUUID: "005056B2A1AC1EEB8586FD178B1BF83D", },
];
let GetCGDData = function (companyList) {
	return myAxios28.get('/qdq/GetCGDData', JSON.stringify({ companyList: companyList }));
};

export {
	GetSuJiRequire
};